import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { BaseItem } from '../../items/BaseItem';
import { Coin } from '../../items/common/Coin';
import { _platform, _player, _resource, _tip } from '../../Main';
import { PropItem } from '../../items/props/PropItem';
import { TT } from '../../platforms/minis/TT';
const { ccclass, property } = _decorator;

@ccclass('GoodItem')
export class GoodItem extends Component {
    
    @property(Sprite)
    itemIcon: Sprite = null!

    @property(Label)
    itemName: Label = null!

    @property(Label)
    itemAmount: Label = null!

    @property(Label)
    coin: Label = null!

    @property(Node)
    buyNode: Node = null!

    @property(Node)
    videoNode: Node = null!

    @property(Node)
    gotNode: Node = null!

    item: BaseItem = null

    amount: number = 0

    init(item: BaseItem, amount: number) {
        this.item = item
        this.amount = amount
        if (!(item instanceof Coin)) {
            this.itemIcon.spriteFrame = this.item.icon
            _resource.spriteFitSquare(this.itemIcon, 90, 90)
        }
        this.itemName.string = this.item.showName
        this.itemAmount.string = '×' + this.amount
        let cost: number = this.item.cost * amount
        this.coin.string = cost + ''
        if (cost <= 0) {
            this.buyNode.active = false
        }
        this.refreshVideo()
    }

    refreshVideo() {
        let b: boolean = _player.getDataNum(this.item.getLimitName(), _player.shopLimits, 2) > 0
        this.videoNode.active = b
        this.gotNode.active = !b
    }

    buy(then?:() => void) {
        if (this.item instanceof PropItem) {
            this.item.buy(this.amount, () => {
                if (then) {
                    then()
                }
            })
        }
    }

    video(then?: () => void) {
        let b: boolean = _player.getDataNum(this.item.getLimitName(), _player.shopLimits, 2) > 0
        if (!b) {
            this.refreshVideo()
            this.got()
            return
        }
        _player.changeNumData(this.item.getLimitName(), _player.shopLimits, -1, 'add', true, () => {
            this.refreshVideo()
            this.item.video(this.amount, () => {
                if (then) {
                    then()
                }
            })
        })
    }

    got() {
        _tip.tip('今日获取次数已达上限')
    }

    btnBuy() {
        this.buy()
    }

    btnVideo() {
        this.video(() => {
            let pf = _platform.platform
            if (pf instanceof TT) {
                pf.reportAnalytics('ShopVideo', {
                    
                })
            }
        })
    }
}


